package xian.woniu.web.controller;

import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import xian.woniu.entity.Appointment;
import xian.woniu.entity.Doctor;
import xian.woniu.entity.Doctor;
import xian.woniu.entity.Orders;
import xian.woniu.entity.Patient;
import xian.woniu.service.*;
import xian.woniu.service.IDoctorService;
import xian.woniu.util.ResultVO;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author bo
 * @time 2022/06/30 16:59:55
 */
@RestController
@RequestMapping("patientinfo")
public class PatientInfoController {

    @Resource
    IPatientService patientService;
    @Resource
    IDoctorService doctorService;
    @Resource
    IOrdersService ordersService;

    /**
     * 查询患者的签约记录
     */
    @PostMapping("selSignRecord/{pid}/{pageNow}/{pageSize}")
    public ResultVO selSignRecord(@PathVariable String pid, @PathVariable Integer pageNow, @PathVariable Integer pageSize) {
        List signList = patientService.selSignRecord(Integer.valueOf(pid), pageNow, pageSize);
        PageInfo<Orders> pageInfo = new PageInfo<Orders>(signList);
        return ResultVO.success("查询成功", pageInfo);
    }

    /**
     * 查询患者的个人信息，返回的是一个list
     */
    @PostMapping("selPatinfoReturnList/{pid}")
    public ResultVO selPatinfoReturnList(@PathVariable String pid) {
        List patinfoList = patientService.selPatinfoReturnList(Integer.valueOf(pid));

        return ResultVO.success("查询成功", patinfoList);
    }
    @PostMapping("selPatinfo1/{pid}")
    public ResultVO selPatinfo1(@PathVariable String pid) {
        Patient patient = patientService.selPatinfo(Integer.valueOf(pid));
        return ResultVO.success("查询成功", patient);
    }
    /**
     * 修改患者的个人信息
     */
    @PostMapping("updatePatinfo")
    public ResultVO updatePatinfo(@RequestBody Patient patient) {
        patientService.updatePatinfo(patient);
        return ResultVO.success("修改成功");
    }

    /**
     * 传入用户的pid，根据用户的家庭地址所在市，查询出这个市的所有医院
     */
    @PostMapping("selLocalDistrictHospital/{pid}")
    public ResultVO selLocalDistrictHospital(@PathVariable String pid) {
        Patient patient = patientService.selPatinfo(Integer.valueOf(pid));
        String districtHospital = patient.getPaddress();
        districtHospital = districtHospital.substring(0, districtHospital.indexOf("市"));
        List districtHospitalList = patientService.selLocalDistrictHospital(districtHospital);
        return ResultVO.success("查询成功", districtHospitalList);
    }

    /**
     * 传入一个省的一个市的，查询出这个市的所有医院
     */
    @PostMapping("selHospital/{districtHospital}")
    public ResultVO selHospital(@PathVariable String districtHospital) {
        List districtHospitalList = patientService.selLocalDistrictHospital(districtHospital);
        return ResultVO.success("查询成功", districtHospitalList);
    }

    /**
     * 查询当前医院可签约的医生团队
     */
    @PostMapping("selSignTeam/{dhospital}")
    public ResultVO selSignTeam(@PathVariable String dhospital) {
        List SignTeamList = patientService.selSignTeam(dhospital);
        return ResultVO.success("查询成功", SignTeamList);
    }

    /**
     * 查询当前医院可签约团队下面的一条套餐详情
     */
    @PostMapping("selSignPackage/{depname}")
    public ResultVO selSignPackage(@PathVariable String depname) {
        depname = depname.substring(0, depname.indexOf("团"));
        List signPackageList = patientService.selSignPackage(depname);
        return ResultVO.success("查询成功", signPackageList);
    }

    /**
     * 查询当前医院可签约团队下面的可签约医生
     */
    @PostMapping("selSignDoc/{dhospital}/{depname}")
    public ResultVO selSignDoc(@PathVariable String dhospital, @PathVariable String depname) {
        Integer depid = patientService.selectByDepname(depname).getDepid();
        List docList = patientService.selSignDoc(depid, dhospital);
        System.out.println("docList = " + docList);
        return ResultVO.success("查询成功", docList);
    }

    /**
     * 添加一条患者的签约记录
     */
    @PostMapping("goSign")
    public ResultVO goSign(@RequestBody Orders orders) {
        Integer otid = orders.getOtid();
        Integer pid = orders.getPid();
        List ordersList = patientService.selSameSign(otid, pid);
        if (ordersList.size() == 0) {
            Integer i = patientService.addSignRecord(orders);

            return ResultVO.success("签约成功");
        }
        return ResultVO.failure("签约失败，已经有同类型套餐");
    }

    /**
     * 续约，已经查询出有相同的在服务中的套餐，那么续约就是把套餐有效期延后一年
     */
    @PostMapping("continueSign")
    public ResultVO continueSign(@RequestBody Orders orders) throws ParseException {
        Integer otid = orders.getOtid();
        Integer pid = orders.getPid();
        DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
        Date startTime = dateFormat1.parse("2009-06-01");
        Date originTime;
        List<Orders> ordersList = patientService.selSameSign(otid, pid);
        for (int i = 0; i < ordersList.size(); i++) {
            originTime = ordersList.get(i).getEndTime();
            if (originTime.getTime() > startTime.getTime()) {
                startTime = originTime;
            }
        }
        orders.setStartTime(startTime);
        patientService.continueSign(orders);
        return ResultVO.success("续约成功");
    }


    //通过uid查个人信息
    @PostMapping("selPatientByUid/{uid}")
    public ResultVO selPatientByUid(@PathVariable("uid") String uid) {
        Patient patient = patientService.selPatientByUid(Integer.valueOf(uid));
        return ResultVO.success("查询成功", patient);
    }

    @PostMapping("regist")
    public ResultVO regist(@RequestBody Patient patient) {
        patientService.addPatinfo(patient);
        return ResultVO.success("添加成功");
    }

    /**
     * 查询所用用户信息
     *
     * @return
     */
    @GetMapping("findAll")
    public ResultVO findAll() {
        return ResultVO.success("查询成功！", patientService.findAll());
    }

    /**
     * 查询一个患者信息
     *
     * @param pid
     * @return
     */
    @GetMapping("findOne/{pid}")
    public ResultVO findOne(@PathVariable String pid) {
        if (StringUtils.isEmpty(pid)) {
            return ResultVO.failure("查询患者信息时，id为空  " + pid);
        }
        Patient one = patientService.findOne(Integer.parseInt(pid));
        return one == null ? ResultVO.failure("患者不存在！", one) : ResultVO.success("查询成功！", one);
    }

    /**
     * 根据患者pid查询出所有和患者签约的医生信息
     */
    @PostMapping("selSignDocByPid/{pid}")
    public ResultVO selSignDocByPid(@PathVariable String pid) {
        List doclist = patientService.selSignDocByPid(Integer.valueOf(pid));
        return ResultVO.success("查询成功", doclist);
    }

    /**
     * 查询出患者的可预约服务时间
     */
    @PostMapping("selAppointmentTime")
    public ResultVO selAppointmentTime() {
        List appointmentTimelist = patientService.selAppointmentTime();
        return ResultVO.success("查询成功", appointmentTimelist);
    }

    /**
     * 查询患者是否在该天已经预约该医生
     */
    @PostMapping("selAppointmentRecord/{pid}/{did}/{atime}")
    public ResultVO selAppointmentRecord(@PathVariable String pid, @PathVariable String did, @PathVariable String atime) {
        Appointment appointment = patientService.selAppointmentRecord(Integer.valueOf(pid), Integer.valueOf(did), atime);
        System.out.println("appointment = " + appointment);
        if (appointment != null) {
            return ResultVO.failure("没有这条记录");
        }
        return ResultVO.success("查询成功,有值");
    }

    /**
     * 患者预约服务，如果已经预约或者出错返回500
     */
    @PostMapping("goAppointment/{pid}/{did}/{atime}")
    public ResultVO goAppointment(@PathVariable String pid, @PathVariable String did, @PathVariable String atime) throws ParseException {
        Appointment appointmentRecord = patientService.selAppointmentRecord(Integer.valueOf(pid), Integer.valueOf(did), atime);
        if (appointmentRecord != null) {
            return ResultVO.failure("已经预约过,不能继续预约");
        }
        Appointment appointment = new Appointment();
        appointment.setPid(Integer.valueOf(pid));
        appointment.setDid(Integer.valueOf(did));
        String astate = "未完成";
        appointment.setAstate(astate);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date atime1 = sdf.parse(atime);
        appointment.setAtime(atime1);
        patientService.goAppointment(appointment);
        return ResultVO.success("没有预约过，可继续预约");
    }

    /**
     * 查询患者的预约记录
     */
    @PostMapping("selAppointmentServiceRecord/{pid}/{pageNow}/{pageSize}")
    public ResultVO selAppointmentServiceRecord(@PathVariable String pid, @PathVariable Integer pageNow, @PathVariable Integer pageSize) {
        List docList = patientService.selAppointmentServiceRecord(Integer.valueOf(pid), pageNow, pageSize);
        PageInfo<Doctor> pageInfo = new PageInfo<Doctor>(docList);
        return ResultVO.success("查询成功", pageInfo);
    }
    //查找患者签约的医生did wangchenxi
    @PostMapping("findDoctorId/{pid}")
    public ResultVO findDoctorId(@PathVariable String pid) {
        ResultVO id = ordersService.findDoctorId(pid);

        return ResultVO.success("查询成功", id);
    }
    //查找患者签约的医生信息 wangchenxi
    @PostMapping("getDoctorByDids")
    public ResultVO getDoctorByUids(@RequestBody Map map){
        ResultVO doctor = doctorService.getDoctorByUids(map);
        return ResultVO.success(doctor);
    }

    /** @CreateBy: hzz
     * @Description: 通过pid查询病人信息
     * @CreateTime: 2022/7/6 14:23
     * @Params: * @Param: null
     * @Return:
     */
    @PostMapping("selPatientByPid/{pid}")
    ResultVO selPatientByPid(@PathVariable("pid") Integer pid) {
        Patient patient = patientService.selPatinfo(pid);
        return ResultVO.success(patient);
    }
    /**
     * 查找患者全部信息包括服务状态
     */
    @GetMapping("getallpatientinfo")
    public ResultVO getAllPatientInfo(){
        List<Patient> patientList = patientService.getAllPatientInfo();
        return ResultVO.success(patientList);
    }
    @GetMapping("getnumbyage")
    public ResultVO getNumbyAge(){
        Map<String, Integer> nummap = patientService.getNumByAge();
        return ResultVO.success(nummap);
    }




    /**
     * 查询患者预约表的各种状态
     */
    @PostMapping("selAppointmentAstate")
    public ResultVO selAppointmentAstate() {
        List astatelist = patientService.selAppointmentAstate();
        return ResultVO.success("查询成功", astatelist);
    }

    /**
     * 根据患者pid和预约状态查询出所有的预约记录
     */
    @PostMapping("selAppointmentByAstate/{pid}/{astate}/{pageNow}/{pageSize}")
    public ResultVO selAppointmentByAstate(@PathVariable String pid, @PathVariable String astate,@PathVariable Integer pageNow, @PathVariable Integer pageSize) {
        List docList = patientService.selAppointmentByAstate(Integer.valueOf(pid), astate,pageNow, pageSize);
        PageInfo<Doctor> pageInfo = new PageInfo<Doctor>(docList);
        return ResultVO.success("查询成功", pageInfo);
    }

    /**
     * 患者取消预约服务，如果已经取消、已完成或者出错返回500
     */
    @PostMapping("cancelAppointment/{pid}/{did}/{atime}")
    public ResultVO cancelAppointment(@PathVariable String pid, @PathVariable String did, @PathVariable String atime) {
        Appointment appointmentAstate = patientService.selAppoinmentAstate(Integer.valueOf(pid), Integer.valueOf(did), atime);
        if (appointmentAstate.getAstate().equals("已完成")|appointmentAstate.getAstate().equals("已取消")) {
            return ResultVO.failure("不是未完成，不能取消");
        }
        Appointment appointment=patientService.selAppointmentRecord(Integer.valueOf(pid), Integer.valueOf(did), atime);
        appointment.setAstate("已取消");
        doctorService.update(appointment);
        return ResultVO.success("未完成的预约已取消");
    }
}